package com.unibuy.smartdevice.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.unibuy.smartdevice.AppCompatActivityAbstract;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.databinding.ActivityRestartHostBinding;
import com.unibuy.smartdevice.exception.LogsEmptyException;
import com.unibuy.smartdevice.exception.LogsParseException;
import com.unibuy.smartdevice.exception.LogsSettingEmptyException;
import com.unibuy.smartdevice.external.HttpAPI;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.HandlerMainScheduler;
import com.unibuy.smartdevice.tools.HandlerMainSchedulerOnClick;
import com.unibuy.smartdevice.tools.ToastHandlerMain;
import com.unibuy.smartdevice.tools.Tools;
import com.unibuy.smartdevice.ui.recycler.RecyclerSettingListAdpter;
import com.unibuy.smartdevice.ui.tools.ImageGlide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RestartHostActivity extends AppCompatActivityAbstract {
    public enum Option {
        TOAST(0),
        BACK_HOME(1),
        IMAGE_RELOAD(2),
        AD_RELOAD(3),
        ;

        private int option;

        public int getOption() {
            return option;
        }

        Option(int option) {
            this.option = option;
        }
    }

    public static final Map<Integer, Option> optionMap = new HashMap<>();
    static {
        for (Option option : Option.values()) {
            optionMap.put(option.getOption(), option);
        }
    }

    private ActivityRestartHostBinding binding;
    private List<String> statusList = new ArrayList<>();
    private RecyclerSettingListAdpter recyclerSettingListAdpter;

    private HttpAPI httpAPI;

    @Override
    protected Context setCtx() {
        return this;
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs()) {
            @Override
            protected void execute(Context context, int commandCode, String message) {
                Option option = optionMap.get(commandCode);

                switch (option) {
                    case TOAST:
                        super.execute(context, commandCode, message);
                        break;
                    case BACK_HOME:
                        MyApp.updateLog(getCtx());
                        getTools().gotoActivity(FontendActivity.class);
                        break;
                    case IMAGE_RELOAD:
                        new ImageReload(this).start();
                        break;
                    case AD_RELOAD:
                        try {
                            httpAPI.downloadAdvertisement();
                        } catch (LogsParseException | LogsSettingEmptyException e) {
                            getLogs().warning(e);
                        }
                        break;
                }
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityRestartHostBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        httpAPI = new HttpAPI(getHandlerMain());

        recyclerSettingListAdpter = new RecyclerSettingListAdpter(statusList);
        binding.recyclerSettingList.setAdapter(recyclerSettingListAdpter);
        binding.recyclerSettingList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        try {
            httpAPI.setIsReloadApi(true);
            httpAPI.downloadSetting();
        } catch (LogsParseException | LogsSettingEmptyException e) {
            getLogs().warning(e);
        }
    }

    private void checkSetting() {
        statusList.add("ByXinYuan comPort:" + MyApp.getInstance().getComPort1ByXinYuan());
        statusList.add("NFCPay comPort:" + MyApp.getInstance().getComPort2ByNFCPay());

        if (!MyApp.getInstance().getMachine().getMachineID().isEmpty()) {
            statusList.add("download maching data:"
                + MyApp.getInstance().getMachine().getMachineID()
                +"/"
                +Tools.randomReplace(MyApp.getInstance().getMachine().getApiKey(), 0.3)
            );
        }

        if (MyApp.getInstance().getGreenInvoice().getGreenMerchantID() != null) {
            statusList.add("download greenpay Invoice data:"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenMerchantID()), 0.3)
                    +"/"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenHashKey()), 0.3)
                    +"/"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenHashIV()), 0.3)
                    +"/"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenInvoice().getGreenEmail()), 0.3)
            );
        }

        if (!MyApp.getInstance().getEsunpay().getStoreID().isEmpty()) {
            statusList.add("download esunpay data:"
                +Tools.randomReplace(MyApp.getInstance().getEsunpay().getStoreID(), 0.3)
                +"/"
                +Tools.randomReplace(MyApp.getInstance().getEsunpay().getTermID(), 0.3)
                +"/"
                +Tools.randomReplace(MyApp.getInstance().getEsunpay().getHash(), 0.3)
            );
        }

        if (MyApp.getInstance().getGreenpaySetting().getAppKey() != null && !MyApp.getInstance().getGreenpaySetting().getAppKey().isEmpty()) {
            statusList.add("download greenpay data:"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenpaySetting().getAppId()), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpaySetting().getAppKey(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpaySetting().getPartnerKey(), 0.3)
            );

            statusList.add("download greenpay merchant data:"
                    +Tools.randomReplace(String.valueOf(MyApp.getInstance().getGreenpayMerchant().getLineMerchantId()), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getPsMerchantId(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getEasyMerchantId(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getPiMerchantId(), 0.3)
                    +"/"
                    +Tools.randomReplace(MyApp.getInstance().getGreenpayMerchant().getJkoMerchantId(), 0.3)
            );
        }

        if (!MyApp.getInstance().getProductList().isEmpty()) {
            int size = MyApp.getInstance().getProductList().size();
            statusList.add("download product data:" + size);
        }

        if (!MyApp.getInstance().getFontendAdUrlString().isEmpty()) {
            int size = MyApp.getInstance().getFontendAdUrlString().size();
            statusList.add("download ad url data:" + size);
        }

        recyclerSettingListAdpter.notifyItemInserted(statusList.size() -1);
    }

    private class ImageReload extends HandlerMainScheduler {
        public ImageReload(HandlerMain handlerMain) {
            super(handlerMain);
        }

        @Override
        protected HandlerMain setHandlerMain() {
            return getSrcHandlerMain();
        }

        @Override
        protected void execute(HandlerMain handlerMain) {
            handlerMain.start(getClass().getSimpleName(),"開始下載商品圖片");

            for (int i = 0; i < MyApp.getInstance().getUploadProductList().size(); i++) {
                ProductStructure product = MyApp.getInstance().getUploadProductList().get(i);
                if (product.getProductImg().isEmpty()) continue;

                try {
                    new ImageGlide(handlerMain.getContext()).preload(product.getProductImg());
                } catch (LogsEmptyException e) {
                    getLogs().warning(e);
                }

                if (i % 10 == 0) {
                    getLogs().info("成功載入" + i + "/" + MyApp.getInstance().getProductList().size() + "筆商品圖片");
                }
            }

            MyApp.getInstance().getUploadProductList().clear();
            handlerMain.start("ImageReload", Option.AD_RELOAD.getOption(), "ad reload");
        }

        @Override
        protected Class<?> setCls() {
            return getClass();
        }
    }
}